import { RequestMethod } from "../lib/defines";
import { RoomBase } from "../utils/types";


/**
 * 申请加入
 *
 * @param app
 */
export default function applyJoin (app) {
  const { socket, rooms } = app.locals;
  const { userId, username } = socket.handshake.query;

  /**
   * 申请加入
   */
  socket.on(RequestMethod.applyJoin, function createApplyJoin (room: RoomBase, cb) {
    const member = { userId, username }
    const __room = rooms[room.classId]
    const creator = __room.creator;

    // 通知创建者，有用户需要加入
    socket
      .timeout(1000 * 60)
      .to(creator.socketId)
      .emit(RequestMethod.applyJoin, { member }, () => {
        cb && cb({ status: 'ok' })
      });
  });
}
